; DeclsInfo:Linked List

;DeleteList ( list_ptr ) : "_DeleteList@4"
;DeleteLink ( lnk_ptr ) : "_DeleteLink@4"
;SetLinkValue ( lnk_ptr, datas$  ) : "_SetLinkValue@8"
;NewList % ( ) : "_NewList@0"
;CopyList % ( List_ptr ) : "_CopyList@4"
;InsertAfter % ( lnk_ptr, after_ptr ) : "_InsertAfter@8"
;InsertBefore % ( lnk_ptr, before_ptr ) : "_InsertBefore@8"
;AddLast % ( list_ptr, pValue$  ) : "_AddLast@8"
;AddFirst % ( list_ptr, pValue$  ) : "_AddFirst@8"
;ListCount % ( list_ptr ) : "_ListCount@4"
;ListFind % ( list_ptr, value$  ) : "_ListFind@8"
;ListIsEmpty % ( list_ptr ) : "_ListIsEmpty@4"
;ListFirst % ( list_ptr ) : "_ListFirst@4"
;ListLast % ( list_ptr ) : "_ListLast@4"
;AfterLink % ( link_ptr ) : "_AfterLink@4"
;BeforeLink % ( link_ptr ) : "_BeforeLink@4"
;LinkCompare % ( l1_ptr, l2_ptr ) : "_LinkCompare@8"
;StrCompare % ( a$ , b$  ) : "_StrCompare@8"
;LinkValue $ ( link_ptr ) : "_LinkValue@4"
;LinkKey $ ( link_ptr ) : "_LinkKey@4"
;SwapLinks % ( l1_ptr, l2_ptr ) : "_SwapLinks@8"
;SortList % ( list_ptr, sens% ) : "_SortList@8"
;LinkList % ( link_ptr ) : "_LinkList@4"


Graphics 400,600,0,2

Local list1=NewList()
	AddLast(list1,"abcd")
	AddLast(list1,"djkq")
	AddLast(list1,"abc")
	AddLast(list1,"bbcd")
	
	SortList list1,1

Print "- Sort List 1 ASC -"
PrintList list1
Print "------------"

Local list2=NewList()
	Print "- Sort List 2 DESC then manipulate links -"

	Print "--------- Initial List ----"
	AddLast(list2,"001")
	AddFirst(list2,"003")
	AddLast(list2,"004")
	AddLast(list2,"002")
	; result : 3,1,4,2
	PrintList list2
	
	Print "--------- Sort List ----"
	SortList list2,-1
	; result : 4,3,2,1
	PrintList list2
	
	Print "--------- Insert 003 after 001"
	Local l1=AfterLink(ListFirst(list2))
	; result : l1 = link("003")
	InsertAfter(l1,ListFind(list2,"001")) ; insert l1 after "001"
	; result : 4,2,1,3
	PrintList list2
	
	
	Print "--------- Swap 002 and 004 then Swap 004 and 003"
	;SwapLinks lnk1,lnk2
	SwapLinks ListFind(list2,"002"),ListFind(list2,"004")
	; result : 2,4,1,3
	SwapLinks ListFind(list2,"004"),ListFind(list2,"003")
	; result : 2,3,1,4
	PrintList list2
	
	Print "--------- Insert 001 before 002"
	InsertBefore ListFind(list2,"001"),ListFind(list2,"002")
	; result : 1,2,3,4
	PrintList list2

	Print "- Modify values of links in List 2 -"
	SetLinkValue ListFirst(list2),"First"
	SetLinkValue AfterLink(ListFirst(list2)),"Second"
	SetLinkValue BeforeLink(ListLast(list2)),"Third"
	SetLinkValue ListLast(list2),"Fourth"
	PrintList list2
	
	Print "- Remove Second Link from list2 -"
	DeleteLink ListFind(list2,"Second")
	PrintList list2

Print "------------"

Local list3=NewList()
	SeedRnd 1
	Local CountObjects=1000
	For n=1 To CountObjects
		AddLast(list3,"Objet "+Str(Rand(1,10000)))
	Next
	Local t0=MilliSecs()
	SortList list3,1
	Local t1=MilliSecs()

Print "- List 3 contains '"+CountObjects+"' objects -"
Print "  time to sort the list : "+(t1-t0)+" ms"

DeleteList(list1)
DeleteList(list2)
DeleteList(list3)

WaitKey
End

Function PrintList(List)
	Local l1=ListFirst(List)
	While l1<>0
		Print "   * "+LinkValue(l1)
		l1=AfterLink(l1)
	Wend
End Function
;~IDEal Editor Parameters:
;~C#Blitz3D